import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.odps.udf.UDF;
import com.aliyun.odps.utils.StringUtils;

/**
 * 解析ewg 成分
 * 
 * @author tuchuanqi
 *
 */
public class SpliderEWGComposition extends UDF {


	public String evaluate(String html) {
		try {
			Document doc = Jsoup.parse(html);
			JSONObject returnObj = new JSONObject();
			// 获取成分名称
			Element nameEle = doc.getElementById("righttoptitleandcats");
			if (null != nameEle && StringUtils.isNotBlank(nameEle.text())) {
				returnObj.put("name", nameEle.text());
			}

			// 获取成分得分 是一张图片链接 取数值  
			//https://static.ewg.org/skindeep/img/draw_score/score_image2_1_1_.png; 1-2
			//https://static.ewg.org/skindeep/img/draw_score/score_image8_8_1_.png; 8
			Elements elementsByClass = doc.getElementsByClass("score2012img");
			if (null != elementsByClass && elementsByClass.size() == 1) {
				Elements elementsByTag = elementsByClass.get(0).getElementsByTag("img");
				if (null != elementsByTag && elementsByTag.size() == 1) {
					String src = elementsByTag.get(0).attr("src");
					String substring = src.substring(src.lastIndexOf("/")+1,src.lastIndexOf("."));
					//获取2_1   8_8那部分
					String partScore = substring.substring(11, 14);
					String[] split = partScore.split("_");
					//因为他的分1-1是1  2-1是1-2  8-8是8 所以要去重
					Set<Integer> set = new HashSet<Integer>();
					for (int i = 0; i < split.length; i++) {
						set.add(Integer.valueOf(split[i]));
					}
					StringBuffer sb = new StringBuffer();
					for (Integer score : set) {
						sb.append(score).append("-");
					}
					String score = (String) sb.subSequence(0, sb.lastIndexOf("-"));
					returnObj.put("score", score);
				}
			}

			// 获取摘要
			Map<String, String> map = new HashMap<String, String>();
			Elements summaryEle = doc.getElementsByClass("additional_notes2012");
			if (null != summaryEle && summaryEle.size() == 1) {
				String text = summaryEle.get(0).text();
				Elements strongTag = summaryEle.get(0).getElementsByTag("strong");
				StringBuffer sb = new StringBuffer();
				if (null != strongTag && strongTag.size() != 0) {
					for (int i = 0; i < strongTag.size(); i++) {
						sb.append(strongTag.get(i).text()).append("|");
					}
				}
				String[] split = text.split(sb.substring(0, sb.lastIndexOf("|")));
				// System.out.println(JSON.toJSONString(split));
				for (int i = 0; i < strongTag.size(); i++) {
					map.put(strongTag.get(i).text(), split[i + 1]);
				}
			}
			Elements summaryEle1 = doc.getElementsByClass("tinytype");
			if (null != summaryEle1 && summaryEle1.size() != 0) {
				for (Element e : summaryEle1) {
					String text = e.text();
					if (StringUtils.isNotBlank(text)) {
						String[] split = text.split(":");
						map.put(split[0], split[1]);
					}
				}
			}
			returnObj.put("summary", map);

			// 获取成分其他描述
			// Persistence and bioaccumulation
			Element pbEle = doc.getElementById("PB");
			Map<String, String> pbMap = commonMap(pbEle);
			if ( null != pbMap) {
				returnObj.put("Persistence and bioaccumulation", pbMap);
			}

			// Organ system toxicity (non-reproductive)
			Element multipEle = doc.getElementById("MULTIPLE");
			Map<String, String> multipMap = commonMap(multipEle);
			if (null != multipMap) {
				returnObj.put("Organ system toxicity", multipMap);
			}

			// Data gaps
			Element dataEle = doc.getElementById("DATA");
			Map<String, String> dataMap = commonMap(dataEle);
			if (null != dataMap) {
				returnObj.put("Data gaps", dataMap);
			}

			// Enhanced skin absorption
			Element absoEle = doc.getElementById("ABSORB");
			Map<String, String> absoMap = commonMap(absoEle);
			if (null != absoMap) {
				returnObj.put("absorption", absoMap);
			}

			// Ecotoxicology
			Element ecoEle = doc.getElementById("ECOTOX");
			Map<String, String> ecoMap = commonMap(ecoEle);
			if (null != ecoMap) {
				returnObj.put("Ecotoxicology", ecoMap);
			}

			// Multiple, additive exposure sources
			Element expEle = doc.getElementById("EXPOSURE");
			Map<String, String> expMap = commonMap(expEle);
			if (null != expMap) {
				returnObj.put("exposure", expMap);
			}

			// Cancer
			Element cancerEle = doc.getElementById("CANCER");
			Map<String, String> cancerMap = commonMap(cancerEle);
			if (null != cancerMap) {
				returnObj.put("Cancer", cancerMap);
			}

			// Data Sources
			Element refEle = doc.getElementById("References");
			if (null != refEle) {
				StringBuffer sb = new StringBuffer();
				Elements bodyTag = refEle.getElementsByTag("tbody");
				if (null != bodyTag && bodyTag.size() == 1) {
					// 获取行
					Elements trTag = bodyTag.get(0).getElementsByTag("tr");
					if (null != trTag && trTag.size() != 0) {
						for (Element trE : trTag) {
							// 获取列
							Elements tdTag = trE.getElementsByTag("td");
							if (null != tdTag && tdTag.size() == 1) {
								sb.append(tdTag.get(0).text()).append("@@");
							}
						}
					}

				}
				returnObj.put("Data Sources", sb.substring(0, sb.lastIndexOf("@@")));
			}

			return JSON.toJSONString(returnObj);
		} catch (Exception e) {
			e.printStackTrace();
			return "解析错误" + e.getMessage();
		}
	}

	private Map<String, String> commonMap(Element e) {
		if (null != e) {
			Map<String, String> trMap = new HashMap<String, String>();
			Elements bodyTag = e.getElementsByTag("tbody");
			if (null != bodyTag && bodyTag.size() == 1) {
				// 获取行
				Elements trTag = bodyTag.get(0).getElementsByTag("tr");
				if (null != trTag && trTag.size() != 0) {
					for (Element trE : trTag) {
						// 获取列
						Elements tdTag = trE.getElementsByTag("td");
						if (null != tdTag && tdTag.size() == 2) {
							trMap.put(tdTag.get(0).text(), tdTag.get(1).text());
						}
					}
				}

			}
			return trMap;
		}
		return null;
	}
}
